global root_dir = "`1'"

include "$root_dir/code/config/config.do"

cap noi log using ${log_dir}/Table_A21_keywords_correlation_periods.log, replace name(tabl)

capture noi { 
	
	* Correlating different classification periods 
	texdoc init ${tab_dir}/Appendix/Table_A21_keywords_correlation_periods.tex, replace
	texdoc stlog, nolog

	cap program drop smpl
	program smpl, rclass
		args a
		local N0 "V6"
		local N1 "V6-1997-2011"
		local N2 "V6-until-1997"
		local N3 "V6-from-1998"
		return local name `N`a''
	end
	*get correct sample per period and store as dta temporarily
	foreach sample in 0 1 2 3 {
		smpl `sample'
		import delimited using ${classification_dir}/`r(name)'/ipc6XX.csv, clear
		tempfile f`sample'
		save `f`sample''
	}
	*collect the samples in one dataset
	clear 
	foreach sample in 0 1 2 3 {
		append using `f`sample''
		keep ipc6
		duplicates drop	
	}
	*get keword numbers per period
	foreach sample in 0 1 2 3 {
		mmerge ipc6 using `f`sample'', unmatched(master) ukeep(share_anyclassification)
		gen share_anyclassification`sample' = share_anyclassification if _m == 3
		drop share_anyclassification
		drop _m
		smpl `sample'
		local period_label `r(name)'
		local period_label: subinstr local period_label "-" "_", all
		ren share_anyclassification`sample' `period_label'
	}
	*get correlations and store in texdoc for the table
	estpost correlate V6* , matrix listwise
	matrix list e(b), format(%9.3f)
	sleep 500
	texdoc local V6_V6 : display %9.3f e(b)[1,1]
	sleep 500
	texdoc local V6_V6_1997_2011 : display %9.3f e(b)[1,2]
	sleep 500
	texdoc local V6_V6_until_1997 : display %9.3f e(b)[1,3]
	sleep 500
	texdoc local V6_V6_from_1998 : display %9.3f e(b)[1,4]
	sleep 500
	texdoc local V6_1997_2011_V6_1997_2011 : display %9.3f e(b)[1,5]
	sleep 500
	texdoc local V6_1997_2011_V6_until_1997 : display %9.3f e(b)[1,6]
	sleep 500
	texdoc local V6_1997_2011_V6_from_1998 : display %9.3f e(b)[1,7]
	sleep 500
	texdoc local V6_until_1997_V6_until_1997 : display %9.3f e(b)[1,8]
	sleep 500
	texdoc local V6_until_1997_V6_from_1998 : display %9.3f e(b)[1,9]
	sleep 500
	texdoc local V6_from_1998_V6_from_1998 : display %9.3f e(b)[1,10]
	sleep 500


	* output table (via texdoc)
	texdoc local notes "Correlation between the prevalence of the main keywords, computed for C/IPC 6-digit codes."
	texdoc stlog close
	qui include ${code_dir}/config/texdoctools.do
	tex `texpreamble'
	/*tex
	\begin{tabular}{@{}lcccc@{}}
	\toprule
	\multicolumn{5}{c}{Prevalence of automation keywords by period} \\
	\midrule
	Keywords  	  & 1978-2017 & 1997-2011 & 1978-1997 & 1998-2017 \\
	\midrule
	1978-2017 	  & `V6_V6' &       &       &       \\
	1997-2011 	  & `V6_V6_1997_2011' & `V6_1997_2011_V6_1997_2011' &       &       \\
	1978-1997 	  & `V6_V6_until_1997' & `V6_1997_2011_V6_until_1997' & `V6_until_1997_V6_until_1997' &   \\
	1998-2017 	  & `V6_V6_from_1998' & `V6_1997_2011_V6_from_1998' & `V6_until_1997_V6_from_1998' & `V6_from_1998_V6_from_1998' \\
	\bottomrule
	\end{tabular}
	tex*/
	tex `texpostamble'
	texdoc close

}
if _rc == 0 {
    display "Execution finished successfully."
}
else {
    display "Execution finished with errors."
}